<?php

declare(strict_types=1);

namespace app\store\controller\game;

use app\store\controller\Controller;
use app\store\service\game\Category as CategoryService;
use think\App;
use think\response\Json;

class Category extends Controller
{
    public  $cateGoryService;
    public function __construct(App $app,CategoryService $cateGoryService)
    {
        parent::__construct($app);
        $this->cateGoryService = $cateGoryService;
    }

    /**
     * @return Json
     */
    public function index(): Json
    {
        $params =  $this->request->get();
        $this->validate(
            $params,
            [
                'page' => 'integer',
                'limit' => 'integer'
            ]
        );
        $list = $this->cateGoryService->getList($params);
        return $this->renderSuccess(['list' => $list]);
    }

    /**
     * @return Json
     */
    public function save():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'id' => 'integer',
                'name' => 'require',
                'sort' => 'integer'
            ]
        );
        $res = $this->cateGoryService->save($params);
        if(!$res){
            return $this->renderError('保存出错');
        }
        return $this->renderSuccess([],'保存成功');

    }

    public function delete():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'id' => 'integer|require'
            ]
        );
        $id = $params['id'];
        $res = $this->cateGoryService->delete($id);
        if(!$res){
            return $this->renderError('删除出错');
        }
        return $this->renderSuccess([],'删除成功');
    }
}